package com.btex.app.treaty;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.btex.app.BaseActivity;
import com.btex.app.Config;
import com.btex.app.R;
import com.btex.app.adapter.MyFragmentAdapter;
import com.btex.app.entity.FuturesListInfo;
import com.btex.app.pager.DealFragment;
import com.btex.app.pager.EntrustHistoryFragment;
import com.btex.app.pager.PositionsFragment;
import com.btex.app.treaty.dialog.HistoryDialog;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HistoryActivity extends BaseActivity {

    @BindView(R.id.activity_counter_imag_back)
    ImageView mActivityCounterImagBack;
    @BindView(R.id.activity_history_tv)
    TextView mActivityHistoryTv;
    @BindView(R.id.activity_history_viewpager)
    ViewPager mActivityHistoryViewpager;
    @BindView(R.id.activity_history_Tablayout)
    TabLayout mActivityHistoryTablayout;
    private List<FuturesListInfo.DataBean> mData;
    public String mType;
    public String mPrice_float_num;
    public String mSa_float_num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        mType = getIntent().getExtras().getString("type");
        getMxCcontracts();
        initViews();
    }

    private void initViews() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new EntrustHistoryFragment());
        fragments.add(new DealFragment());
        fragments.add(new PositionsFragment());
        ArrayList<String> strings = new ArrayList<>();
        strings.add(getString(R.string.heyue_lishi_weituo));
        strings.add(getString(R.string.heyue_chengjiao));
        strings.add(getString(R.string.heyue_yiping_cangwei));


        mActivityHistoryViewpager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(), fragments, strings));
        mActivityHistoryViewpager.setOffscreenPageLimit(3);


        mActivityHistoryTablayout.setSelectedTabIndicatorHeight(5);

        mActivityHistoryTablayout.setupWithViewPager(mActivityHistoryViewpager);
    }

    @Override
    public void setData() {

    }

    @Override
    public void onClick(View v) {

    }

    @OnClick({R.id.activity_counter_imag_back, R.id.activity_history_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_counter_imag_back:
                finish();
                break;
            case R.id.activity_history_tv:

                HistoryDialog historyDialog = new HistoryDialog(this);
                historyDialog.setData(mData);
                historyDialog.show();
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void HistoryActivity(String event) {


        if ("HistoryDialog".equals(event)) {
            mActivityHistoryTv.setText(Config.CONTRACT_ID_TYPE);
//            getMxCcontracts();

        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    private void getMxCcontracts() {
        int num = (int) ((Math.random() * 9 + 1) * 100000);


        OkGo.<String>get(Config.GET_MX_CONTRACTS + "?rnd=" + String.valueOf(num)).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String result = response.body().toString();

                Log.i("eruheywfhyw", result);

                try {

                    JSONObject jsonObj = new JSONObject(result);
                    if (jsonObj.getString("code").equals("1")) {

//                        setList(responseInfo.result);
                        Gson gson = new Gson();
                        FuturesListInfo futuresInfo = gson.fromJson(result, FuturesListInfo.class);
                        setTitle(futuresInfo);
                        mData = futuresInfo.getData();

                    } else {

                    }


                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }
        });


//        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(this);
//        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(this, sharedPreferences);
//        RequestParams params = new RequestParams();
//        params.addHeader("app-version", Config.VERSION);
//        params.addHeader("channel", MyApplication.mApp.getApplicationMetaData("CXD_CHANNEL_ID"));
//        params.addHeader("my-client", "android");
//
//        MyApplication.get().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.GET, Config.GET_MX_CONTRACTS + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//
//                Log.i("eruheywfhyw", responseInfo.result);
//
//                try {
//
//                    JSONObject jsonObj = new JSONObject(responseInfo.result);
//                    if (jsonObj.getString("code").equals("1")) {
//
////                        setList(responseInfo.result);
//                        Gson gson = new Gson();
//                        FuturesListInfo futuresInfo = gson.fromJson(responseInfo.result, FuturesListInfo.class);
//                        setTitle(futuresInfo);
//                        mData = futuresInfo.getData();
//
//                    } else {
//
//                    }
//
//
//                } catch (JSONException e) {
//                    e.printStackTrace();
//
//                }
//
//
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//            }
//
//        });

    }

    private void setTitle(FuturesListInfo futuresInfo) {

        for (int i = 0; i < futuresInfo.getData().size(); i++) {

            if (mType.equals(futuresInfo.getData().get(i).getContract_id())) {
                String Pair = futuresInfo.getData().get(i).getPair();
                mPrice_float_num = futuresInfo.getData().get(i).getPrice_float_num();
                mSa_float_num = futuresInfo.getData().get(i).getSa_float_num();
                mActivityHistoryTv.setText(Pair);

                Log.i("weoifwoejfwoiefjowi", mPrice_float_num + "==========" + mSa_float_num);
            }

        }
    }


}
